//
//  LSZPostQuestionsVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/19.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZPostQuestionsVC.h"

@interface LSZPostQuestionsVC ()<UITextViewDelegate,LDImagePickerDelegate>
@property(nonatomic,strong) UITextView  * textViewQuestions;
@property(nonatomic,assign) NSInteger chooseWhatImageView;
@property(nonatomic,strong) NSString * imageStrOne;
@property(nonatomic,strong) NSString * imageStrTwo;
@property(nonatomic,strong) NSString * imageStrThree;

@property(nonatomic,strong) UIImageView * imageViewOne;
@property(nonatomic,strong) UIImageView * imageViewTwo;
@property(nonatomic,strong) UIImageView * imageViewThree;
@end

@implementation LSZPostQuestionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageStrOne = @"";
    self.imageStrTwo = @"";
    self.imageStrThree = @"";

    self.title = @"发布问题";
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;

    self.textViewQuestions = [[UITextView alloc]init];
    self.textViewQuestions.frame = CGRectMake(10, 10+NAVIGATION_BAR_HEIGHT, ScreenWidth-20, 200);
    self.textViewQuestions.delegate = self;
    [self.textViewQuestions setFont:[UIFont systemFontOfSize:15]];
    [self.textViewQuestions setTextColor:themeColor];
    [self.view addSubview:self.textViewQuestions];
    
    UIView * viewImaview = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.textViewQuestions.frame), ScreenWidth-20, 80)];
    viewImaview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewImaview];
    
    self.imageViewOne = [[UIImageView alloc]init];
    self.imageViewOne.frame = CGRectMake(5, 5, 70, 70);
    self.imageViewOne.image = [UIImage imageNamed:@"添加图片"];
    [viewImaview addSubview:self.imageViewOne];
    [self.imageViewOne tapGesture:^(UIGestureRecognizer *ges) {
        [self ChooseOneClick];
    }];
    self.imageViewTwo = [[UIImageView alloc]init];
    self.imageViewTwo.image = [UIImage imageNamed:@"添加图片"];
    self.imageViewTwo.hidden = YES;
    self.imageViewTwo.frame = CGRectMake(5+CGRectGetMaxX(self.imageViewOne.frame),  5, 70, 70);
    [viewImaview addSubview:self.imageViewTwo];
    [self.imageViewTwo tapGesture:^(UIGestureRecognizer *ges) {
        [self ChooseTwoClick];
    }];
    
    self.imageViewThree = [[UIImageView alloc]init];
    self.imageViewThree.hidden = YES;
    self.imageViewThree.image = [UIImage imageNamed:@"添加图片"];
    self.imageViewThree.frame = CGRectMake(5+CGRectGetMaxX(self.imageViewTwo.frame),  5, 70, 70);
    [viewImaview addSubview:self.imageViewThree];
    [self.imageViewThree tapGesture:^(UIGestureRecognizer *ges) {
        [self ChooseThreeClick];
    }];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickPostQuestionsVC)];
    [self.view addGestureRecognizer:tapGesture];
}
-(void)textViewDidChange:(UITextView *)textView{
    CGFloat  maxHeight = ScreenHeight/1.0-167.f-40.f;
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    if (size.height<=frame.size.height) {
        size.height=frame.size.height;
    }else{
        if (size.height >= maxHeight)
        {
            size.height = maxHeight;
            textView.scrollEnabled = YES;  // 允许滚动
        }
        else
        {
            textView.scrollEnabled = NO;    // 不允许滚动
        }
    }
    textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}
#pragma mark - 发布按钮
-(void)onClickedOKbtn
{
    if ((NullStringjudgment(self.textViewQuestions.text)).length == 0) {
        [self initWithtring:@"请先填写发布的内容!"];
        return;
    }
    NSString * strUrl = [NSString stringWithFormat:@""];
   
    if ((NullStringjudgment(NSStr(self.imageStrThree))).length > 0) {
        strUrl = [NSString stringWithFormat:@"%@,%@,%@",self.imageStrOne,self.imageStrTwo,self.imageStrThree];
    }else if ((NullStringjudgment(NSStr(self.imageStrTwo))).length > 0){
        strUrl = [NSString stringWithFormat:@"%@,%@",self.imageStrOne,self.imageStrTwo];

    }else if ((NullStringjudgment(NSStr(self.imageStrOne))).length > 0) {
        strUrl = [NSString stringWithFormat:@"%@",self.imageStrOne];

    }else{
        
    }
    [LSZRequestData submitQuestions:self.textViewQuestions.text coverUrl:strUrl:^(BOOL isOK, NSString * str, NSDictionary * diction) {
        if (isOK == YES) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:str];
        }
    }];
}

#pragma mark - 上传图片
-(void)ChooseOneClick
{
    self.chooseWhatImageView = 1;
    [self ModifyThePicture];
}
-(void)ChooseTwoClick
{
    self.chooseWhatImageView = 2;
    [self ModifyThePicture];
}
-(void)ChooseThreeClick
{
    self.chooseWhatImageView = 3;
    [self ModifyThePicture];
}
#pragma mark - 修改头像
-(void)ModifyThePicture
{
    __weak typeof(self) weakSelf = self;
    [weakSelf tapClickPostQuestionsVC];
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"相册",@"拍照"] actionSheetBlock:^(NSInteger buttonIndex) {
        if(buttonIndex ==0) {
            [weakSelf initWithPhoneFromThePhotoAlbum];
        }else if (buttonIndex == 1){
            [weakSelf initWithPhoneTakingpictures];
        }
    }];
    [actionSheet show];
}
#pragma mark - 拍照
-(void)initWithPhoneTakingpictures
{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else {
        [self initWithtring:@"此设备没有摄像功能!"];
        return;
    }
    // 跳转到相机或相册页面
    LDImagePicker *imagePicker = [LDImagePicker sharedInstance];
    imagePicker.delegate = self;
    //设置宽高比scale来设置剪切框大小，剪切框宽度固定为屏幕宽度
    [imagePicker showImagePickerWithType:0   InViewController:self Scale:1];
}
#pragma mark - 从相册里面查找
-(void)initWithPhoneFromThePhotoAlbum
{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //来源:相册//来自图库
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
            //无权限
            [self initWithtring:@"照片访问受限,请允许app访问你的照片!"];
            return;
        }
    }
    else {
        //来自相册
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
            //无权限
            [self initWithtring:@"照片访问受限,请允许app访问你的照片!"];
            return;
        }
    }
    // 跳转到相机或相册页面
    LDImagePicker *imagePicker = [LDImagePicker sharedInstance];
    imagePicker.delegate = self;
    //设置宽高比scale来设置剪切框大小，剪切框宽度固定为屏幕宽度
    [imagePicker showImagePickerWithType:1   InViewController:self Scale:1];
}
- (void)imagePicker:(LDImagePicker *)imagePicker didFinished:(UIImage *)editedImage{
    UIImage *imagestr = editedImage;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@".png"]];   // 保存文件的名称
    BOOL result = [UIImagePNGRepresentation(imagestr)writeToFile: filePath    atomically:YES]; // 保存成功会返回YES
    if (self.chooseWhatImageView == 1) {
        self.imageViewOne.hidden = NO;
        self.imageViewOne.image = editedImage;
        self.imageViewTwo.hidden = NO;
    }else if (self.chooseWhatImageView == 2){
        self.imageViewTwo.image = editedImage;
        self.imageViewThree.hidden = NO;
    }else{
        self.imageViewThree.image = editedImage;
        
    }
    //需要在此处上传服务器
    __weak typeof(self) weakSelf = self;
    [LSZRequestData UploadFile:filePath imageView:editedImage :^(BOOL isOk, NSString * string, NSString * str) {
        if (isOk == YES) {
            if (self.chooseWhatImageView == 1) {
                self.imageStrOne = [NSString stringWithFormat:@"%@%@",URLTotalWeb,str];
            }else if (self.chooseWhatImageView == 2){
                self.imageStrTwo = [NSString stringWithFormat:@"%@%@",URLTotalWeb,str];
            }else{
                self.imageStrThree = [NSString stringWithFormat:@"%@%@",URLTotalWeb,str];
            }
        }else{
            [weakSelf initWithtring:@"图片上传失败!"];
        }
    }];
}
-(void)tapClickPostQuestionsVC
{
    [self.view endEditing:YES];
}
@end
